/*
 * PreConfig.java
 *
 * Version information
 *
 * Aug 26, 2005
 * 
 * Copyright (c) AmSoft Systems, 2006
 */

package net.amsoft.iservice.ilink.webapp.struts.action;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.ilink.webapp.dataObject.ILink;
import net.amsoft.iservice.ilink.webapp.dataObject.LoginData;
import net.amsoft.iservice.ilink.webapp.struts.form.ConfigForm;
import net.amsoft.iservice.ilink.util.Dictionary;
import net.amsoft.iservice.ilink.hibernate.Redirect;
import net.amsoft.iservice.ilink.service.ILinkService;
import net.amsoft.iservice.ilink.util.ILinkConstants;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * This action class is used to configure the I-Links of the i-name.
 * 
 * @author mohit
 * 
 */

public class PreConfig extends BaseAction {
	private static final Logger oLogger = Logger.getLogger(PreConfig.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse) {
		oLogger.info("process() : entry");

		// Check if user is logged in
		LoginData oLoginData = (LoginData) oRequest.getSession(false)
				.getAttribute(ILinkConstants.SESSION_LOGIN_OBJ);
		if (oLoginData.isLoginStatus() == false) {
			oLogger.debug("process() : Login Status: false");
			return oMapping.findForward(ILinkConstants.FWD_CHKLOGIN);
		}

		ConfigForm oConfigForm = new ConfigForm();

		// Set redicts
		ArrayList alRedirect = new ArrayList();
		try {
			alRedirect = ILinkService.getRedirects(oLoginData
					.getLoggedInSynonym());

			// Set Synonynms of logged in user
			oConfigForm.setSynonyms(ILinkService.getAllSynonyms(oLoginData
					.getLoggedInSynonym()));
		} catch (IServiceException ise) {
			oLogger.warn("process() : Exception : " + ise.getMessage());
		}

		Redirect oRedirect = null;

		ILink[] oILink = new ILink[alRedirect.size()];
		int i = 0;

		// Iterate over saved redirects to create custom links
		for (Iterator oIter = alRedirect.iterator(); oIter.hasNext();) {
			oRedirect = (Redirect) oIter.next();
			String sXriNet;

			// Check for ending slash
			if (!ILinkConstants.URL_XRI_NET.endsWith("/"))
				sXriNet = ILinkConstants.URL_XRI_NET + "/";
			else
				sXriNet = ILinkConstants.URL_XRI_NET;

			String sCustomLink = sXriNet + oRedirect.getSynonym().getSynonym()
					+ "/" + "(" + oRedirect.getTag() + ")";

			oILink[i++] = new ILink(sCustomLink, oRedirect.getWebResource(),
					oRedirect.getPublic());
		}
		oConfigForm.setIlink(oILink);

		// Set dictionary tags
		oConfigForm.setDictionaryPersonal(Dictionary.getDictionary(0));
		oConfigForm.setDictionaryBusiness(Dictionary.getDictionary(1));
		oConfigForm.setDictionaryCommunity(Dictionary.getDictionary(2));

		// Save object in request
		oRequest.setAttribute(ILinkConstants.REQUEST_CONFIG_OBJ, oConfigForm);

		oLogger.info("process() : Object set forwarding to configure.jsp");

		if (oRequest.getParameter(ILinkConstants.REQ_ATTRIBUTE_SCOPE) != null) {
			if (oRequest.getParameter(ILinkConstants.REQ_ATTRIBUTE_SCOPE)
					.equals("ilinks"))
				return oMapping.findForward(ILinkConstants.FWD_ILINKS);
		}
		return oMapping.findForward(ILinkConstants.FWD_SUCCESS);
	}
}